﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using telWebBrowserNet.Lib;
using telWebBrowserNet.Lib.TeleDLL;
using telWebBrowserNet.Lib.TeleDLL.Model;

namespace telWebBrowserNet
{
    public partial class Form1 : Form
    {
        private int isOpenDevice = 0;
        public Form1()
        {
            InitializeComponent();
            LogInfo.setForm1(this);
            InitializeWebView();
            openDevice();
        }

        private void InitializeWebView()
        {
            // 假设webview2是已创建好的WebView2控件实例
            string url = "http://localhost:5173/#/recorder";
            webViewMain.Source = new Uri(url);
        }

        public void SetLog(string log)
        {
            if (txtLogs.Text.Length > 30000)
            {
                txtLogs.Text = "";
            }
            txtLogs.Text = log+txtLogs.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openDevice();
        }

        private void openDevice()
        {
            TeleBaseModel teleBaseModel = TeleHelper.StartBox();
            if (teleBaseModel.Code == 0)
            {
                this.isOpenDevice = 1;
                telTimer.Enabled = true;
            }
        }

        private void txtRead_Click(object sender, EventArgs e)
        {
            ReadModel readMode = BoxCommand.CheckStatus();
            txtLogs.Text += " 状态：" + readMode.Station;
            if (readMode.Station == "CallInRing")
            {
                BoxCommand.HookOff();
            }
        }

        private async void TelTimerIntval(object sender, EventArgs e)
        {
            ReadModel readMode = BoxCommand.CheckStatus();
            txtLogs.Text += " 状态：" + readMode.Station;
            if (readMode.Station == "CallInRing")
            {
                BoxCommand.HookOff();
                if (webViewMain.CoreWebView2 != null)
                {
                    webViewMain.CoreWebView2.Reload();
                    var result = await webViewMain.CoreWebView2.ExecuteScriptAsync("telHoldOff()");
                }
            }
        }

        private void btnWebRefresh_Click(object sender, EventArgs e)
        {
            if (webViewMain.CoreWebView2 != null)
            {
                webViewMain.CoreWebView2.Reload();
            }
        }
    }
}
